﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EvincePayroll.Data;
using EvincePayroll.Utility;

namespace EvincePayroll.Logic
{
    public class UserRoleLogic:BaseLogic
    {
        public UserRoleLogic(string userId)
            : base(userId)
        { }
        //Completed
        public List<UserRoleData> GetUserRoleList(string userId)
        {
            try
            {
       //         return db.GetUserRoleDataList(userId);
                return db.GetUserRoleDataList().OrderBy(x => x.UserRole).ToList();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        
        //Completed
        public bool DeleteRole(string roleId)
        {
            try
            {
                return db.DeleteUserRole(roleId);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        public List<MenuTableData> GetMenuList(string emailId,string roleId)
        {
            List<MenuTableData> menuList = null;
            try
            {
                var menuTableList = db.GetMenuTableDataList();
                menuList = (from info in menuTableList
                            //join menu in roleMenuManagerList on info.MenuId.ToString() equals menu.MenuId.ToString()
                            select new MenuTableData
                            {
                             MenuId=info.MenuId,
                             MenuName=info.MenuName,
                             URL=info.URL,
                             Title=info.Title,
                             Description=info.Description,
                             DisplayOrder=GetDisplayOrder(info.MenuId,roleId),
                             ParentId=GetParentId(info.MenuId,roleId),
                             IsActive=info.IsActive,
                             isChecked=getSelectedMenu(info.MenuId,roleId)
                            }).OrderBy(x => x.DisplayOrder).ToList<MenuTableData>();
                //db.GetMenuTableDataList();

               // return db.GetMenuTableDataList();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

            return menuList;
        }

        public string GetParentId(Guid menuId, string roleId)
        {
            string result = string.Empty;
            result = db.GetRoleMenuManagerDataList().Where(x => x.RoleId.ToString().Equals(roleId) && x.MenuId.Equals(menuId)).Select(x => x.ParentId).FirstOrDefault();
            return result;
        }
        public Int32 GetDisplayOrder(Guid menuId, string roleId)
        {
            Int32 result = 0;
            var temp = db.GetRoleMenuManagerDataList().Where(x => x.RoleId.ToString().Equals(roleId) && x.MenuId.Equals(menuId)).Select(x => x.SortOrder).FirstOrDefault();
            if(temp!=null)
                result = temp;
            return result;
        }
        public bool getSelectedMenu(Guid menuId, string roleId)
        {
            bool result = false;
            var menuIdList = db.GetRoleMenuManagerDataList().Where(x => x.RoleId.ToString().Equals(roleId) && x.MenuId.Equals(menuId)).ToList();
            if (menuIdList.Count != 0)
                result = true;
            else
                result = false;
            return result;
        }

        public bool UpdateRoleAccess(string menuId, string roleId, string sortOrder, string parentId)
        {
            try
            {
                Guid MenuId = new Guid(menuId);
                Guid RoleId = new Guid(roleId);
                RoleMenuManagerData roleMenuManagerData = new RoleMenuManagerData();
                if (MenuId != null && RoleId != null)
                {
                    roleMenuManagerData.MenuId = MenuId;
                    roleMenuManagerData.RoleId = RoleId;
                    roleMenuManagerData.SortOrder = Convert.ToInt32(sortOrder);
                    roleMenuManagerData.ParentId = parentId;
                }

                return db.CreateRoleMenuManager(roleMenuManagerData);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }


        }


        public List<DynamicMenu> DynamicMenu(EmployeeData employeeData)
        {
            List<DynamicMenu> result = null;
            try
            {
                var menuTableList = db.GetMenuTableDataList();
                var roleMenuManagerList = db.GetRoleMenuManagerDataList().Where(x=>x.RoleId.Equals(employeeData.UserRoleId));
                result = (from data in roleMenuManagerList
                          select new DynamicMenu
                          {
                              MenuId=data.MenuId,
                              MenuName=menuTableList.Where(x=>x.MenuId.Equals(data.MenuId)).Select(x=>x.Title).FirstOrDefault(),
                              URL = menuTableList.Where(x => x.MenuId.Equals(data.MenuId)).Select(x => x.URL).FirstOrDefault(),
                              ParentId=data.ParentId,
                              SortOrder=data.SortOrder
                          }).OrderBy(x=>x.SortOrder).ToList<DynamicMenu>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

            return result;
        }

        public bool DeleteRoleAccess(string menuId, string roleId)
        {
            bool result = false;
            try
            {
                Guid MenuId = new Guid(menuId);
                Guid RoleId = new Guid(roleId);
                
                var roleMenuManagerId = db.GetRoleMenuManagerDataList().Where(x=>x.MenuId.Equals(MenuId) && x.RoleId.Equals(RoleId)).Select(x=>x.RoleMenuManagerId).FirstOrDefault();
                if(roleMenuManagerId!=Guid.Empty)
                    result = db.DeleteRoleMenuManager(roleMenuManagerId.ToString());
                
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

            return result;
        }

        public bool CreateMenuList(MenuTableData menuTableData, string email)
        {
            bool result = false;
            try
            {
                if (menuTableData != null)
                {
                    return db.CreateMenuTable(menuTableData);
                    
                
                
                }
                result = true;

            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

            return result;
        }

        public bool DeleteMenu(string menuId)
        {

            bool result = false;
            try
            {
                if (menuId != null)
                {
                    return db.DeleteMenuItem(menuId);



                }
                result = true;

            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

            return result;
        
        }
    }
}
 